package com.atguigu.srb.base.listener;

public class Animal {
    private String name;
    private AnimalListener animalListener;

    public Animal(String name) {
        this.name = name;
    }

    public void addAnimalListener(AnimalListener animalListener) {
        this.animalListener = animalListener;
    }

    public void eat() {
        System.out.println("3 eat()" + name + " 3");
        animalListener.eatEventHandler(new AnimalEvent(this));
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static void main(String[] args) {
        Animal dog = new Animal("dog");
        // 为事件源添加监听器
        AnimalListener listener = new AnimalListener();
        dog.addAnimalListener(listener);

        // 触发事件，会被事件监听器监听到
        dog.eat();
    }
}
